package org.javaymw.springboot.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.apache.commons.lang3.StringUtils;
import org.javaymw.springboot.dto.sys.SLogDto;
import org.javaymw.springboot.entity.sys.SLogEntity;
import org.javaymw.springboot.mapper.sys.SLogMapper;
import org.javaymw.springboot.service.SLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：
 * <p>
 * Author: 赵新国
 * Date: 2017/10/24 18:36
 */
@Service
@Transactional
public class SLogServiceImpl extends ServiceImpl<SLogMapper, SLogEntity> implements SLogService {

    private static final Logger logger = LoggerFactory.getLogger(SLogServiceImpl.class);

    @Autowired
    private SLogMapper sLogMapper;

    @Override
    public void saveSLog(SLogDto sLogDto) {
        SLogEntity sLogEntity = new SLogEntity();
        BeanUtils.copyProperties(sLogDto, sLogEntity);
        sLogMapper.insert(sLogEntity);
    }

    @Override
    public Page<SLogEntity> getLogByPage(int pageNum, int pageSize, String requestType, String startDate, String endDate) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> columnMap = new HashMap<String, Object>(3);
        if (!StringUtils.isBlank(requestType)) {
            columnMap.put("request_type", requestType);
        }
        if (!StringUtils.isBlank(startDate)) {
            columnMap.put("startDate", startDate);
        }
        if (!StringUtils.isBlank(endDate)) {
            columnMap.put("endDate", endDate);
        }
        Page<SLogEntity> logEntities = sLogMapper.findLogByPage(columnMap);
        if (logEntities == null) {
            return null;
        }
        return logEntities;
    }

    @Override
    public List<SLogDto> getLogs(String requestType, String startDate, String endDate) {
        // 从 DB 中获取用户列表信息
        EntityWrapper<SLogEntity> entityWrapper = new EntityWrapper<SLogEntity>();
        entityWrapper.like("request_type", requestType);
        if (!startDate.isEmpty()) {
            entityWrapper.ge(true, "ts_insert", startDate);
        }
        if (!endDate.isEmpty()) {
            entityWrapper.le(true, "ts_insert", endDate);
        }
        List<SLogEntity> logEntities = sLogMapper.selectList(entityWrapper);
        if (logEntities == null || logEntities.isEmpty()) {
            return null;
        }
        List<SLogDto> logDtos = toDtos(logEntities);
        return logDtos;
    }

    private List<SLogDto> toDtos(List<SLogEntity> logEntities) {
        List<SLogDto> logDtos = new ArrayList<SLogDto>();
        for (SLogEntity logEntity : logEntities) {
            SLogDto logDto = new SLogDto();
            BeanUtils.copyProperties(logEntity, logDto);
            logDtos.add(logDto);
        }
        return logDtos;
    }

}
